package com.zadtek.shopmall.fragments;

import android.Manifest;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.goyourfly.multi_picture.ImageLoader;
import com.goyourfly.multi_picture.MultiPictureView;
import com.iarcuschin.simpleratingbar.SimpleRatingBar;
import com.zadtek.shopmall.R;
import com.zadtek.shopmall.adapter.MyAdapter;
import com.zadtek.shopmall.base.AbsFragment;
import com.zadtek.shopmall.bean.EvalutionItem;
import com.zadtek.shopmall.bean.PictureItem;
import com.zadtek.shopmall.bean.ProductEvaluateBean;
import com.zadtek.shopmall.net.BaseObserver;
import com.zadtek.shopmall.net.BaseResponse;
import com.zadtek.shopmall.net.NetTool;
import com.zadtek.shopmall.utils.ToastUtil;
import com.zadtek.shopmall.view.NoScrollView;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Zackv on 2018/4/17.
 */

public class ProductEvaluationFragment extends AbsFragment implements View.OnClickListener {
    private RecyclerView recyclerView;
    private EvaluationAdapter adapter;
    private LinearLayout llAllEvaluation;
    private TextView tvAll;
    private TextView tvAllEvalution;
    private LinearLayout llGoodEvaluation;
    private TextView tvGood;
    private TextView tvGoodEvaluation;
    private LinearLayout llMidEvaluation;
    private TextView tvMid;
    private TextView tvMidEvaluation;
    private LinearLayout llBadEvaluation;
    private TextView tvBad;
    private TextView tvBadEvaluation;
    private LinearLayout llPicEvaluation;
    private TextView tvPic;
    private TextView tvPicEvaluation;

    private int currenSelect = 0;
    private List<EvalutionItem> data0 = new LinkedList<>();
    private List<EvalutionItem> data1 = new LinkedList<>();
    private List<EvalutionItem> data2 = new LinkedList<>();
    private List<EvalutionItem> data3 = new LinkedList<>();
    private List<EvalutionItem> data4 = new LinkedList<>();
    private int pagesize = 10;
    private int page = 1;
    private ProductEvaluateBean productEvaluateBean;
    @Override
    public int setView() {
        return R.layout.fragment_product_evaluation;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        initView();
        initLogic();
        requestData();
    }

    private void requestData() {
//        productEvaluateBean = new ProductEvaluateBean();
//        EvalutionItem item = new EvalutionItem();
//        item.setUserName("星艺度空山");
//        item.setUserAvatar("https://www.fotor.com/images2/features/crop/crop_2.jpg");
//        item.setTimestamp("2018-05-23 08:23:31");
//        item.setStartNum(4);
//        item.setContent("这个商品真的很不错，十分好。");
//        List<String> list2 = new ArrayList<>();
//        list2.add("http://img02.tooopen.com/images/20160509/tooopen_sy_161967094653.jpg");
//        list2.add("http://img02.tooopen.com/images/20160509/tooopen_sy_161967094653.jpg");
//        list2.add("http://img02.tooopen.com/images/20160509/tooopen_sy_161967094653.jpg");
//        list2.add("http://img02.tooopen.com/images/20160509/tooopen_sy_161967094653.jpg");
//        item.setImgs(list2);
//        List<EvalutionItem> list = new LinkedList<>();
//        list.add(item);
//        list.add(item);
//        list.add(item);
//        productEvaluateBean.setEvalutionItems(list);
//        BaseResponse<ProductEvaluateBean> response = new BaseResponse<>();
//        response.setMessage("");
//        response.setCode(0);
//        response.setData(productEvaluateBean);
//        Gson gson = new Gson();
//        String js = gson.toJson(response);
        loadData0();
    }

    private void loadData4() {
        Map<String, Object> param = NetTool.newParams();
        NetTool.getApi().getProductEvaluateInfo(param)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<ProductEvaluateBean>>() {
                    @Override
                    public void onData(BaseResponse<ProductEvaluateBean> value) {
                        data4.addAll(value.getData().getEvalutionItems());
                        if(data4.size()>20){
                            adapter.loadMoreEnd();
                        }else{
                            adapter.loadMoreComplete();
                        }
                        adapter.notifyDataSetChanged();
                        //tvPicEvaluation.setText(String.valueOf(data4.size()));
                    }
                });
    }

    private void loadData3() {
        Map<String, Object> param = NetTool.newParams();
        NetTool.getApi().getProductEvaluateInfo(param)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<ProductEvaluateBean>>() {
                    @Override
                    public void onData(BaseResponse<ProductEvaluateBean> value) {
                        data3.addAll(value.getData().getEvalutionItems());
                        if(data3.size()>20){
                            adapter.loadMoreEnd();
                        }else{
                            adapter.loadMoreComplete();
                        }
                        adapter.notifyDataSetChanged();
                        //tvBadEvaluation.setText(String.valueOf(data3.size()));
                    }
                });
    }

    private void loadData2() {
        Map<String, Object> param = NetTool.newParams();
        NetTool.getApi().getProductEvaluateInfo(param)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<ProductEvaluateBean>>() {
                    @Override
                    public void onData(BaseResponse<ProductEvaluateBean> value) {
                        data2.addAll(value.getData().getEvalutionItems());
                        if(data2.size()>20){
                            adapter.loadMoreEnd();
                        }else{
                            adapter.loadMoreComplete();
                        }
                        adapter.notifyDataSetChanged();
                        //tvMidEvaluation.setText(String.valueOf(data2.size()));
                    }
                });
    }

    private void loadData1() {
        Map<String, Object> param = NetTool.newParams();
        NetTool.getApi().getProductEvaluateInfo(param)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<ProductEvaluateBean>>() {
                    @Override
                    public void onData(BaseResponse<ProductEvaluateBean> value) {
                        data1.addAll(value.getData().getEvalutionItems());
                        if(data1.size()>20){
                            adapter.loadMoreEnd();
                        }else{
                            adapter.loadMoreComplete();
                        }
                        adapter.notifyDataSetChanged();
                        //tvGoodEvaluation.setText(String.valueOf(data1.size()));
                    }
                });
    }

    private void loadData0() {

        Map<String, Object> param = NetTool.newParams();
        param.put("page",page);
        param.put("pageszie",pagesize);
        param.put("type",0);
        param.put("productId",1);
        NetTool.getApi().getProductEvaluateInfo(param)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<ProductEvaluateBean>>() {
                    @Override
                    public void onData(BaseResponse<ProductEvaluateBean> value) {
                        data0.addAll(value.getData().getEvalutionItems());
                        if(data0.size()>20){
                            adapter.loadMoreEnd();
                        }else{
                            adapter.loadMoreComplete();
                            adapter.notifyDataSetChanged();
                            //tvAllEvalution.setText(String.valueOf(data0.size()));
                        }
                        tvAllEvalution.setText(String.valueOf(value.getData().getTotalNum()));
                        tvGoodEvaluation.setText(String.valueOf(value.getData().getGoodNum()));
                        tvMidEvaluation.setText(String.valueOf(value.getData().getGoodNum()));
                        tvBadEvaluation.setText(String.valueOf(value.getData().getBadNum()));
                        tvPicEvaluation.setText(String.valueOf(value.getData().getPicNum()));
                    }
                });
    }

    private void initLogic() {

        MultiPictureView.setImageLoader(new ImageLoader() {
            @Override
            public void loadImage(@NotNull ImageView image, @NotNull Uri uri) {
                String path = uri.getPath().substring(1);
                Glide.with(mActivity).load(path).placeholder(R.drawable.placeholder).into(image);
            }
        });
        adapter = new EvaluationAdapter(R.layout.evaluate_detail_item,data0);
        recyclerView.setAdapter(adapter);
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                switch (currenSelect){
                    case 0:
                        loadData0();
                        break;
                    case 1:
                        loadData1();
                        break;
                    case 2:
                        loadData2();
                        break;
                    case 3:
                        loadData3();
                        break;
                    case 4:
                        loadData4();
                        break;
                }
            }
        },recyclerView);
        adapter.setEmptyView(R.layout.normal_empty_view);
        llAllEvaluation.setOnClickListener(this);
        llBadEvaluation.setOnClickListener(this);
        llGoodEvaluation.setOnClickListener(this);
        llMidEvaluation.setOnClickListener(this);
        llPicEvaluation.setOnClickListener(this);
    }

    private void initView() {
        llAllEvaluation = (LinearLayout)findViewById( R.id.ll_all_evaluation );
        tvAll = (TextView)findViewById( R.id.tv_all );
        tvAllEvalution = (TextView)findViewById( R.id.tv_all_evalution );
        llGoodEvaluation = (LinearLayout)findViewById( R.id.ll_good_evaluation );
        tvGood = (TextView)findViewById( R.id.tv_good );
        tvGoodEvaluation = (TextView)findViewById( R.id.tv_good_evaluation );
        llMidEvaluation = (LinearLayout)findViewById( R.id.ll_mid_evaluation );
        tvMid = (TextView)findViewById( R.id.tv_mid );
        tvMidEvaluation = (TextView)findViewById( R.id.tv_mid_evaluation );
        llBadEvaluation = (LinearLayout)findViewById( R.id.ll_bad_evaluation );
        tvBad = (TextView)findViewById( R.id.tv_bad );
        tvBadEvaluation = (TextView)findViewById( R.id.tv_bad_evaluation );
        llPicEvaluation = (LinearLayout)findViewById( R.id.ll_pic_evaluation );
        tvPic = (TextView)findViewById( R.id.tv_pic );
        tvPicEvaluation = (TextView)findViewById( R.id.tv_pic_evaluation );


        recyclerView = (RecyclerView) findViewById(R.id.recycleview);
        recyclerView.setNestedScrollingEnabled(false);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mActivity);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ll_all_evaluation:
                select(0);
                break;
            case R.id.ll_good_evaluation:
                select(1);
                break;
            case R.id.ll_mid_evaluation:
                select(2);
                break;
            case R.id.ll_bad_evaluation:
                select(3);
                break;
            case R.id.ll_pic_evaluation:
                select(4);
                break;

        }
    }

    private void select(int i) {
        currenSelect = i;
        int colorNormal = getResources().getColor(R.color.color_text_black5nd);
        int colorSelect = getResources().getColor(R.color.color_red);
        //reset all
        tvAll.setTextColor(colorNormal);
        tvAllEvalution.setTextColor(colorNormal);
        tvGood.setTextColor(colorNormal);
        tvGoodEvaluation.setTextColor(colorNormal);
        tvMid.setTextColor(colorNormal);
        tvMidEvaluation.setTextColor(colorNormal);
        tvBad.setTextColor(colorNormal);
        tvBadEvaluation.setTextColor(colorNormal);
        tvPic.setTextColor(colorNormal);
        tvPicEvaluation.setTextColor(colorNormal);

        switch (i){
            case 0:
                tvAll.setTextColor(colorSelect);
                tvAllEvalution.setTextColor(colorSelect);
                adapter.setNewData(data0);
                page = data0.size()/pagesize+1;
                if(data0.size() == 0){loadData0();}
                break;
            case 1:
                tvGood.setTextColor(colorSelect);
                tvGoodEvaluation.setTextColor(colorSelect);
                adapter.setNewData(data1);
                page = data1.size()/pagesize+1;
                if(data1.size() == 0){
                    loadData1();
                }
                break;
            case 2:
                tvMid.setTextColor(colorSelect);
                tvMidEvaluation.setTextColor(colorSelect);
                adapter.setNewData(data2);
                page = data2.size()/pagesize+1;
                if(data2.size() == 0){
                    loadData2();
                }
                break;
            case 3:
                tvBad.setTextColor(colorSelect);
                tvBadEvaluation.setTextColor(colorSelect);
                adapter.setNewData(data3);
                page = data3.size()/pagesize+1;
                if(data3.size() == 0){
                    loadData3();
                }
                break;
            case 4:
                tvPic.setTextColor(colorSelect);
                tvPicEvaluation.setTextColor(colorSelect);
                adapter.setNewData(data4);
                page = data0.size()/pagesize+1;
                if(data4.size() == 0){
                    loadData4();
                }
                break;
        }
        ToastUtil.showToast(String.valueOf(page));

    }

    private class EvaluationAdapter extends BaseQuickAdapter<EvalutionItem,BaseViewHolder>{

        public EvaluationAdapter(int layoutResId, @Nullable List<EvalutionItem> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(final BaseViewHolder helper, EvalutionItem item) {
//            NoScrollView gv = helper.getView(R.id.gl_img);
//            gv.setAdapter(new MyAdapter<String>((ArrayList<String>) item.getImgs(),R.layout.image_view) {
//                @Override
//                public void bindView(MyAdapter.ViewHolder holder, String obj) {
//                    Glide.with(mActivity).load(obj).placeholder(R.drawable.placeholder).into((ImageView) holder.getView(R.id.iv_img));
//                }
//            });
            MultiPictureView multiPictureView = helper.getView(R.id.multiview);
            List<Uri> uris = new ArrayList<>();
            for(String i:item.getImgs()){
                Uri uri = new Uri.Builder().appendPath(i).build();
                uris.add(uri);
            }
            multiPictureView.setList(uris);
            Glide.with(mActivity).load(item.getUserAvatar()).placeholder(R.drawable.placeholder).into((ImageView) helper.getView(R.id.iv_avatar));
            helper.setText(R.id.tv_username,item.getUserName());
            helper.setText(R.id.tv_evaluate_content,item.getContent());
            helper.setText(R.id.tv_timestamp,item.getTimestamp());
            SimpleRatingBar simpleRatingBar = helper.getView(R.id.rb_mark);
            simpleRatingBar.setRating(item.getStartNum());
        }
    }
}
